package com.bugpool.leilema.tc.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bugpool.leilema.tc.bean.entity.SysRoleDept;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wh
 * @Description 角色部门mapper
 * @Date 2023/6/12 16:51
 * @Version 1.0
 **/
@Mapper
public interface RoleDeptMapper extends BaseMapper<SysRoleDept> {
    /**
     * 根据角色id 查询出所有的部门id
     *
     * @param roleIds
     * @return
     */
    default List<Long> getDeptsByRoleId(List<Long> roleIds) {
        QueryWrapper<SysRoleDept> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(SysRoleDept::getRoleId, roleIds);
        return this.selectList(wrapper)
                .stream()
                .map(SysRoleDept::getDeptId)
                .distinct()
                .collect(Collectors.toList());
    }

    /**
     * 根据角色id 删除绑定关系
     *
     * @param roleId
     */
    default void deleteByRoleId(Long roleId) {
        UpdateWrapper<SysRoleDept> wrapper=new UpdateWrapper<>();
        wrapper.lambda().eq(SysRoleDept::getRoleId,roleId);
        this.delete(wrapper);
    }
}
